package pl.edu.pw.perg.compinfodb.ui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;

import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.application.support.AbstractView;

import pl.edu.pw.perg.compinfodb.command.AddEventCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.ReloadEventsCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.SaveEventsCommandExecutor;
import pl.edu.pw.perg.compinfodb.command.SerachModeCommand;
import pl.edu.pw.perg.compinfodb.datamodel.ConnectorTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.ElementTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.HistoryTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.LogicalDetailsTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.SearchTableModel;
import pl.edu.pw.perg.compinfodb.datamodel.StructureTreeModel;

public class StructureManagerView extends AbstractView {

	private StructureTreeModel structureTreeModel;
//	private IHistoryManager historyManager;
	private ElementTableModel equipmentTableModel;
	private HistoryTableModel historyTableModel;
	private CustomRendererTableFactory tableFactory = new CustomRendererTableFactory();
	private ConnectorTableModel inputConnectorTableModel = ConnectorTableModel.INPUT;
	private ConnectorTableModel outputConnectorTableModel = ConnectorTableModel.OUTPUT;
//	private SerachModeCommand searchModeCommand;
	private SearchTableModel searchTableModel = new SearchTableModel();
	private AddEventCommandExecutor addEventExecutor;
	private SaveEventsCommandExecutor saveEventsCommandExecutor;
	private ReloadEventsCommandExecutor reloadEventsCommandExecutor;
	
	@Override
	protected JComponent createControl() {

		JPanel view = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.weighty = 0.5;
		c.gridx = 0;
		c.gridy = 0;
		c.gridheight = 1;
		c.gridwidth = 1;
		LogicalDetailsTableModel ldtm = new LogicalDetailsTableModel();
		JTable elementsTable = tableFactory.getElementTable(equipmentTableModel);
		JTable propertiesTable = tableFactory.getTable(ldtm);
		JTable historyTable = tableFactory.getTable(historyTableModel);
		JTree structureTree = new JTree(structureTreeModel);
		JTable inputConnectionsTable = tableFactory.getTable(inputConnectorTableModel);
		JTable outputConnectionsTable = tableFactory.getTable(outputConnectorTableModel);
		JScrollPane inputConnectionsScrPane = new JScrollPane(inputConnectionsTable);
		JScrollPane outputConnectionsScrPane = new JScrollPane(outputConnectionsTable);
		structureTree.setCellRenderer( new EquipmentTreeCellRenderer(getIconSource()));
		structureTree.setRootVisible(false);
		structureTree.setShowsRootHandles(true);
		structureTreeModel.addSelectionListener(ldtm);
		structureTree.addTreeSelectionListener(structureTreeModel);
		structureTreeModel.addSelectionListener(equipmentTableModel);
		equipmentTableModel.addPhysicalElementSelectionListener(historyTableModel);
		structureTreeModel.addSelectionListener(inputConnectorTableModel);
		structureTreeModel.addSelectionListener(outputConnectorTableModel);

		JTable search = new JTable(searchTableModel);
		searchTableModel.addTableModelListener(search);
		equipmentTableModel.addColumnModelListener(searchTableModel);
		searchTableModel.addSearchFilterListener(equipmentTableModel);
		equipmentTableModel.recreateColumnModel();
		equipmentTableModel.addPhysicalElementSelectionListener(addEventExecutor);
		historyTableModel.addEventModifiactionListener(saveEventsCommandExecutor);
		historyTableModel.addEventModifiactionListener(reloadEventsCommandExecutor);
//		search.setColumnModel(elementsTable.getColumnModel());
		JScrollPane treeScrPane = new JScrollPane(structureTree);
		JScrollPane elementsScrPane = new JScrollPane(elementsTable);
		JScrollPane propertiesScrPane = new JScrollPane(propertiesTable);
		JScrollPane historyScrPane = new JScrollPane(historyTable);
		search.setTableHeader(null);
		JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, search, elementsScrPane);
		jsp.setEnabled(false);


		JSplitPane verticalLeftSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, treeScrPane, propertiesScrPane);
		JSplitPane connectorsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, inputConnectionsScrPane, outputConnectionsScrPane);
		JSplitPane downRightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, connectorsSplitPane, historyScrPane);
		JSplitPane verticalRightSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, downRightSplitPane);	
		JSplitPane horizontalSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, verticalLeftSplitPane,verticalRightSplitPane);
		
		
		verticalLeftSplitPane.setDividerLocation(450);
		verticalRightSplitPane.setDividerLocation(350);
		horizontalSplitPane.setDividerLocation(250);
		downRightSplitPane.setDividerLocation(200);
		connectorsSplitPane.setDividerLocation(450);
		view.add(horizontalSplitPane, c);
		return view;
	}
	
	@Override
	protected void registerLocalCommandExecutors(PageComponentContext context) {
//		context.register("searchModeCommand", searchModeCommand);
		context.register("addEventCommand", addEventExecutor);
		context.register("saveEventsCommand", saveEventsCommandExecutor);
		context.register("reloadEventsCommand", reloadEventsCommandExecutor);
	}


	public void setEquipmentTableModel(ElementTableModel equipmentTableModel) {
		this.equipmentTableModel = equipmentTableModel;
	}


	public void setStructureTreeModel(StructureTreeModel structureTreeModel) {
		this.structureTreeModel = structureTreeModel;
	}


	public void setHistoryTableModel(HistoryTableModel historyTableModel) {
		this.historyTableModel = historyTableModel;
		addEventExecutor = new AddEventCommandExecutor(historyTableModel);
		reloadEventsCommandExecutor = new ReloadEventsCommandExecutor(historyTableModel);
		saveEventsCommandExecutor = new SaveEventsCommandExecutor(historyTableModel);
	}

}
